<?php
/**
 * Created by Yury Patrin.
 * Time: 04.12.13 16:44
 */

class User_model extends CI_Model
{
    const CODE_USER_CREATE_DONE             = '00';
    const CODE_USER_EXISTS_ERROR            = '01';
    const CODE_USER_EMAIL_ERROR             = '04';
    const CODE_USER_PASSWD_EMPTY_ERROR      = '07';

    protected $user_id;

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    public function getUsersList()
    {   
        
        $users = $this->db->get('users')->result();
        return $users;
    }

    public function getUsersByEmailMask($mask)
    {
        $users = $this->db->like('email', $mask)->get('users')->result();
        return $users;
    }

    public function getUserById($id)
    {
        $user = $this->db->where('id', $id)->get('users')->row();
        return $user;
    }

    public function setUserId($id)
    {
        $this->user_id = $id;
        return $this;
    }

    public function setUserField($field, $val)
    {
        if ($field == '' OR $val == '')
            return false;

        $this->db->where('id', $this->user_id);
        $this->db->update('users', [$field => $val]);

        return true;
    }

    public function createUser($email, $password)
    {
        if (!filter_var($email, FILTER_VALIDATE_EMAIL))
        {
            return ['status' => 'error', 'resCode' => User_model::CODE_USER_EMAIL_ERROR];
        }

        if ($this->isUserExists($email))  //check if email already registered
        {
            return ['status' => 'error', 'resCode' => User_model::CODE_USER_EXISTS_ERROR];
        }
        else
        {
            if ($password == '')
                return ['status' => 'error', 'resCode' => User_model::CODE_USER_PASSWD_EMPTY_ERROR];

            $regDate = date("Y-m-d");
            $regTime = date("H:i:s");

            $this->db->insert('users', [
                                              'email' => $email,
                                              'passwd' => md5($password),
                                              'reg_date' => $regDate,
                                              'reg_time' => $regTime,
                                              'active'  => 'Y',
                                          ]);

            $userID = $this->db->insert_id();;

            $MailChimp = new MailChimp();
            $MailChimp->call('lists/subscribe', array(
                'id'                => 'b486f9e04c',
                'email'             => array('email' => $email),
                'double_optin'      => false,
                'update_existing'   => true,
                'replace_interests' => false,
                'send_welcome'      => false,
            ));

            return ['status' => 'error', 'resCode' => User_model::CODE_USER_CREATE_DONE];
        }

    }

    public function isUserExists($email)
    {
        $query = $this->db->get_where('users', array('email' => $email));

        foreach ($query->result() as $user)
        {
            if ($user->id)
                return true;
            else
                return false;
        }
    }

    public function deleteUser()
    {
        $this->db->where('id', $this->user_id);
        $this->db->delete('users');
    }
}